﻿using NPOI.SS.UserModel;

namespace StarsAdmin.Core.Helpers
{
    public static class ExcelHelper
    {
        /// <summary>
        /// 获取头部样式
        /// </summary>
        /// <param name="workbook"></param>
        /// <returns></returns>
        public static ICellStyle GetHeaderStyle(this IWorkbook workbook)
        {
            // 创建样式
            var headerStyle = workbook.CreateCellStyle();
            headerStyle.VerticalAlignment = VerticalAlignment.Center; // 垂直居中
            headerStyle.Alignment = HorizontalAlignment.Center;        // 水平居中

            // 创建字体
            var headerFont = workbook.CreateFont();
            headerFont.Color = IndexedColors.Black.Index;              // 设置字体颜色为黑色
            headerFont.FontName = "宋体";
            headerFont.IsBold = true;
            headerFont.FontHeightInPoints = 11;

            // 应用字体到样式
            headerStyle.SetFont(headerFont);

            // 设置边框线
            headerStyle.BorderBottom = BorderStyle.Thin;
            headerStyle.BorderLeft = BorderStyle.Thin;
            headerStyle.BorderRight = BorderStyle.Thin;
            headerStyle.BorderTop = BorderStyle.Thin;

            return headerStyle;
        }

        /// <summary>
        /// 获取通用列样式
        /// </summary>
        /// <param name="workbook"></param>
        /// <returns></returns>
        public static ICellStyle GetCommonStyle(this IWorkbook workbook)
        {
            // 创建样式
            var commonCellStyle = workbook.CreateCellStyle();
            commonCellStyle.VerticalAlignment = VerticalAlignment.Center; // 垂直居中
            commonCellStyle.Alignment = HorizontalAlignment.Center;        // 水平居中

            // 创建字体
            var commonFont = workbook.CreateFont();
            commonFont.FontName = "宋体";
            commonFont.FontHeightInPoints = 10;

            // 应用字体到样式
            commonCellStyle.SetFont(commonFont);

            // 设置边框线
            commonCellStyle.BorderBottom = BorderStyle.Thin;
            commonCellStyle.BorderLeft = BorderStyle.Thin;
            commonCellStyle.BorderRight = BorderStyle.Thin;
            commonCellStyle.BorderTop = BorderStyle.Thin;

            return commonCellStyle;
        }
    }
}